package cn.ztydata.teachingsystem.heaven.web.controller;

import cn.ztydata.teachingsystem.exception.ServiceException;
import cn.ztydata.teachingsystem.heaven.cons.MsgTemplate;
import cn.ztydata.teachingsystem.heaven.entity.CourseCode;
import cn.ztydata.teachingsystem.heaven.service.CourseCodeService;
import cn.ztydata.teachingsystem.heaven.web.dto.CourseCodeDto;
import cn.ztydata.teachingsystem.webservice.result.json.JsonResult;
import cn.ztydata.teachingsystem.webservice.result.json.JsonResultId;
import cn.ztydata.teachingsystem.webservice.result.json.JsonResultList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.text.MessageFormat;
import java.util.List;

/**
 * 课程教务代码控制器类
 * Created by chenxin on 2014/12/9.
 */
@Controller
@RequestMapping("/lessonCode")
public class LessonCodeController extends BaseController {
    @Autowired
    private CourseCodeService courseCodeService;

    /**
     * 新增课程教务代码
     * @param courseCodeDto 课程教务代码dto类
     * @return json
     *
     * @author cx
     * @since 2014-12-09
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult save(@Valid @RequestBody CourseCodeDto courseCodeDto) {
        //映射dto类到实体类
        CourseCode courseCode = mapper.map(courseCodeDto, CourseCode.class);

        //新增课程教务代码
        try {
            courseCodeService.save(courseCode);
        } catch (ServiceException ex) {
            log.debug("新增课程教务代码失败");
            return new JsonResultId(false, ex.getMessage().isEmpty() ? "保存失败" : ex.getMessage());
        }

        log.debug("新增课程教务代码成功");
        return new JsonResultId(courseCode.getId());
    }

    /**
     * 课程教务代码编辑
     * @param courseCodeDto 课程教务代码dto类
     * @return json
     *
     * @author cx
     * @since 2014-12-09
     */
    @RequestMapping(value = "/edit", method = RequestMethod.PUT)
    @ResponseBody
    public JsonResult update(@Valid @RequestBody CourseCodeDto courseCodeDto) {
        //映射dto类到实体类
        CourseCode courseCode = mapper.map(courseCodeDto, CourseCode.class);

        //编辑课程教务代码
        try {
            courseCodeService.update(courseCode);
        } catch (ServiceException ex) {
            log.debug("课程教务代码编辑失败");
            return new JsonResult(false, ex.getMessage().isEmpty() ? "保存失败" : ex.getMessage());
        }

        log.debug("课程教务代码编辑成功");
        return new JsonResult();
    }

    /**
     * 删除课程教务代码
     * @param id 教务代码ID
     * @return json
     *
     * @author cx
     * @since 2014-12-09
     */
    @RequestMapping(value = "/remove/{id}/{courseId}", method = RequestMethod.DELETE)
    @ResponseBody
    public JsonResult delete(@PathVariable long id, @PathVariable long courseId) {
        //校验参数
        Assert.isTrue(id > 0, MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "教务代码ID"));

        //删除课程教务代码
        try {
            courseCodeService.delete(id, courseId);
        } catch (ServiceException ex) {
            log.debug("删除课程教务代码失败");
            return new JsonResult(false, ex.getMessage().isEmpty() ? "删除失败" : ex.getMessage());
        }

        log.debug("删除课程教务代码成功");
        return new JsonResult();
    }

    /**
     * 获取课程的教务代码
     * @param courseId 课程ID
     * @return json
     *
     * @author cx
     * @since 2014-12-09
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public JsonResultList<CourseCodeDto> getAll(@RequestParam("id") long courseId) {
        //校验参数
        Assert.isTrue(courseId > 0, MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "课程ID"));

        //获取教务代码集合
        List<CourseCode> courseCodeList = courseCodeService.getAll(courseId);
        //映射到dto类
        List<CourseCodeDto> resultList = mapper.mapList(courseCodeList, CourseCodeDto.class);

        log.debug("获取课程的教务代码");
        return new JsonResultList<>(resultList);
    }
}
